<template>
  <div>
    <el-dialog
      :title="ruleForm.id == '' ? '添加鱼池' : '编辑鱼池'"
      :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="鱼类名称：" prop="fishName">
              <el-select
                v-model="ruleForm.fishName"
                placeholder="请选择鱼类名称"
                clearable
              >
                <el-option
                  v-for="item in fishArray"
                  :key="item.id"
                  :label="item.fishName"
                  :value="item.fishName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="鱼类编码：" prop="fishCode">
              <el-select
                v-model="ruleForm.fishCode"
                placeholder="请选择鱼类编码"
                clearable
              >
                <el-option
                  v-for="item in fishTypeArrary"
                  :key="item.id"
                  :label="item.fishName"
                  :value="item.fishCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="鱼性别：" prop="sex">
              <el-select
                v-model="ruleForm.sex"
                placeholder="请选择鱼性别"
                clearable
              >
                <el-option
                  v-for="item in sexArrary"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="鱼类批次：" prop="batchCode">
              <el-select
                v-model="ruleForm.batchCode"
                placeholder="请选择鱼类批次"
                clearable
              >
                <el-option
                  v-for="item in fishBatchsArrary1"
                  :key="item.id"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="鱼尾数：" prop="num">
              <el-input-number
                v-model="ruleForm.num"
                placeholder="请输入数量"
                :precision="0"
                :step="1"
                :max="9999"
                :min="0"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卵径：" prop="diameter">
              <el-select
                v-model="ruleForm.diameter"
                placeholder="请选择卵径："
                clearable
              >
                <el-option
                  v-for="item in DiametersArrary"
                  :key="item.id"
                  :label="item.diameter"
                  :value="item.diameter"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="初重：" prop="weightPrime">
              <el-input-number
                v-model="ruleForm.weightPrime"
                placeholder="请输入初重"
                :precision="2"
                :step="1"
                :max="9999"
                :min="0"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总重：" prop="weight">
              <el-input-number
                v-model="ruleForm.weight"
                placeholder="请输入总重"
                :precision="2"
                :step="1"
                :max="9999"
                :min="0"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row> </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">

import { getObjFishpondDetail, saveObjFishpondDetail } from "@/api/fishpond";
import { getObjFishBatchs, getObjFishs } from "@/api/godown";
import { getObjFishponds,getObjFishTypes ,getObjDiameters} from "@/api/breeding";
import { getSysFishTypes } from "@/api/system";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    fishpondId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      sexArrary: [{ id: 0, name: '未鉴定' }, { id: 1, name: '雄' }, { id: 2, name: '雌' },],
      ruleForm: {
        // 表单字段
        id: "",
        fishpondId: "",
        fishTypeId: "",
        fishTypeYear: "",
        fishNum: "",
        fishEggSize: "",
        fishInitWeight: "",
        fishNowWeight: "",
        fishAverageWeight: "",
      },
      rules: {
        // 表单规则
        fishTypeId: [{ required: true, message: "请选择鱼类名称", trigger: "change" }],
        fishTypeYear: [{ required: true, message: "请输入鱼类年份", trigger: "blur" }],
        fishNum: [{ required: true, message: "请输入数量", trigger: "blur" }],
        fishEggSize: [{ required: true, message: "请输入卵径", trigger: "blur" }],
        fishInitWeight: [{ required: true, message: "请输入初重", trigger: "blur" }],
        fishNowWeight: [{ required: true, message: "请输入当重", trigger: "blur" }],
        fishAverageWeight: [{ required: true, message: "请输入当重", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      fishTypeArray: [], // 鱼类集合
      userType: "", // 用户类型（0超级管理员，1公司管理员）
      fishBatchsArrary: [],
      fishBatchsArrary1: [],
      fishTypeArrary: [],
      DiametersArrary:[],
      fishArray: []
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // this.ruleForm.fishpondId = this.fishpondId;
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        this.userType = Number(window.localStorage.getItem("userType"));
        this.getSysFishTypes();
      }
    },
  },
  mounted() {
    this.getObjFishBatchs()
    this.getObjFishs()
    this.getObjFishponds()
    this.getObjFishTypes()
    this.getObjDiameters()
    this.ruleForm.fishpondId = this.$route.query.id
  },
  methods: {
    // 获取全部鱼池
    getObjFishponds() {
      getObjFishponds(this.$route.query).then((res) => {
        if (res.code == 200) {
          this.fishBatchsArrary = res.data;
        }
      });
    },
    getObjDiameters() {
      getObjDiameters().then((res) => {
        if (res.code == 200) {
          this.DiametersArrary = res.data;
        }
      });
    },
    getObjFishTypes() {
      getObjFishTypes({id: this.$route.query.companyId}).then((res) => {
        if (res.code == 200) {
          this.fishTypeArrary = res.data;
        }
      });
    },
    //获取全部鱼
    getObjFishs() {
      getObjFishs({ id: this.$route.query.companyId }).then((res) => {
        if (res.code == 200) {
          this.fishArray = res.data;
        }
      });
    },
    // 详情
    getDetail() {
      getObjFishpondDetail({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;
          this.getSysFishTypes();
        }
      });
    },
    // 获取全部鱼批次（公司id）
    getObjFishBatchs() {
      // console.log(this.$route.query);
      getObjFishBatchs({ id: this.$route.query.companyId }).then((res) => {
        if (res.code == 200) {
          this.fishBatchsArrary1 = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveObjFishpondDetail(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    getSysFishTypes() {
      getSysFishTypes().then((res) => {
        if (res.code == 200) {
          this.fishTypeArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

  .el-cascader {
    width: 100%;
  }
}

.address_icon {
  width: 30px;
  height: 30px;
  margin-left: 225px;
  margin-top: -35px;
  display: block;
  cursor: pointer;
}
</style>
